"""扫描记录模型"""
from sqlalchemy import Column, Integer, String, DateTime, JSON, ForeignKey, func
from sqlalchemy.orm import relationship
from app.database import Base


class ScanRecord(Base):
    """扫描记录表"""
    __tablename__ = "scan_records"

    id = Column(Integer, primary_key=True, index=True)
    task_id = Column(Integer, ForeignKey("scan_tasks.id"), nullable=False, comment="任务ID")
    branch = Column(String(255), nullable=False, comment="扫描分支")
    scan_time = Column(DateTime, nullable=False, comment="扫描时间")
    status = Column(String(20), nullable=False, comment="状态: success/failed/partial")
    duration = Column(Integer, comment="扫描耗时(秒)")
    scan_params = Column(JSON, comment="扫描参数")
    issue_count = Column(Integer, default=0, comment="问题总数")
    confirmed_count = Column(Integer, default=0, comment="已确认问题数")
    unconfirmed_count = Column(Integer, default=0, comment="未确认问题数")
    created_by = Column(String(100), comment="创建人")
    created_at = Column(DateTime, server_default=func.now(), comment="创建时间")
    updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now(), comment="更新时间")

    # 关系
    task = relationship("ScanTask", backref="scan_records")
    issues = relationship("Issue", back_populates="scan_record", cascade="all, delete-orphan")

